package com.fysports.web.kit.upload;

import com.google.common.collect.Maps;
import com.jfinal.log.Log;
import com.jfinal.upload.UploadFile;
import com.fysports.framework.exception.FrameException;
import com.fysports.web.kit.commons.FileKit;

import java.util.Map;


/**
 * Class title:  默认上传组件 <br/>
 * Describe:                    <br/>
 * Created by konbluesky        <br/>
 * Date : 15/12/10 下午5:21      <br/>
 * Project : jdmdataServer      <br/>
 */
public class DefaultUploadHelper extends UploadHelper {
    private static Log logger = Log.getLog(DefaultUploadHelper.class);
    private String module;
    private UploadFile file;
    private String filename;
    private String bizuuid;

    /**
     * 初始化
     *
     * @param module 上传目录标志
     * @param bizuuid 业务uuid
     * @para f 文件
     */
    public DefaultUploadHelper(UploadFile f,String module,String bizuuid) {
        this.file = f;
        this.module = module;
        this.bizuuid=bizuuid;
    }


    @Override
    public void upload() {
        try {
            filename = FileKit.saveFileForTmp(file.getFile(), module, bizuuid);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(),e);
            throw new FrameException(e);
        }
    }


    @Override
    public Object getResult() {
        Map<String, Object> obj = Maps.newHashMap();
        obj.put("name", filename);
        obj.put("size", file.getFile().length());
        obj.put("type", file.getContentType());
        obj.put("url", createURL(module,bizuuid)+"/"+filename);
        obj.put("thumbnail_url", createURL(module,bizuuid)+"/"+filename);
        obj.put("delete_url", createURL(module,bizuuid)+"/"+filename);
        obj.put("delete_type", "GET");
        return obj;
    }

}
